-----------------------------------
-- Area: The Shrine of Ru'Avitau
-- NPC:  Kirin
-----------------------------------
package.loaded[ "scripts/zones/The_Shrine_of_RuAvitau/TextIDs" ] = nil;
-----------------------------------

require( "scripts/zones/The_Shrine_of_RuAvitau/TextIDs" );
require( "scripts/globals/titles" );
require( "scripts/globals/status" );

-----------------------------------
-- onMobInitialize Action
-----------------------------------
function onMobInitialize( mob )
    -- Add regen and gain effects..
	mob:addStatusEffect( EFFECT_REGEN, 50, 3, 0 );
	mob:addStatusEffect( EFFECT_REGAIN, 15, 3, 0 );
end

-----------------------------------
-- onMobFight Action
-----------------------------------
function onMobFight( mob, target )
    if (mob:getBattleTime() ~= 0 and mob:getBattleTime() % 180 == 0) then
        -- Ensure we have not spawned all pets yet..
        local genbu, seiryu, byakko, suzaku = mob:getExtraVar( 4 );
        if (genbu == 1 and seiryu == 1 and byakko == 1 and suzaku == 1) then
            return;
        end
        
        -- Pick a pet to spawn at random..
        local ChosenPet = nil;
        repeat
        
            local rand = math.random( 0, 3 );
            ChosenPet = 17506671 + rand;
            
            switch (ChosenPet): caseof {
                [17506671] = function (x) if ( genbu == 1) then ChosenPet = 0; else  genbu = 1; end end, -- Genbu
                [17506672] = function (x) if (seiryu == 1) then ChosenPet = 0; else seiryu = 1; end end, -- Seiryu
                [17506673] = function (x) if (byakko == 1) then ChosenPet = 0; else byakko = 1; end end, -- Byakko
                [17506674] = function (x) if (suzaku == 1) then ChosenPet = 0; else suzaku = 1; end end, -- Suzaku
            }
            
        until (ChosenPet ~= 0 and ChosenPet ~= nil)
        
        -- Spawn the pet..
        local pet = SpawnMob( ChosenPet );
        pet:updateEnmity( target );
        pet:setPos( mob:getXPos(), mob:getYPos(), mob:getZPos() );

        -- Update Kirins extra vars..
        mob:setExtraVar( genbu, seiryu, byakko, suzaku );
    end

    -- Ensure all spawned pets are doing stuff..
    for pets = 17506671, 17506674 do
        if (GetMobAction( pets ) == 16) then 
            -- Send pet after current target..
            GetMobByID( pets ):updateEnmity( target );
        end
    end
end

-----------------------------------
-- onMobDeath
-----------------------------------
function onMobDeath( mob, killer )
    -- Award title and cleanup..
    killer:addTitle( KIRIN_CAPTIVATOR );
    killer:showText( mob, KIRIN_OFFSET + 1 );
    GetNPCByID( 17506693 ):hideNPC( 900 );
    
    -- Despawn pets..
	DespawnMob( 17506671 );
	DespawnMob( 17506672 );
	DespawnMob( 17506673 );
	DespawnMob( 17506674 );
    
    -- Reset popped pet var..
    mob:setExtraVar( 0 );
end

-----------------------------------
-- OnMobDespawn
-----------------------------------
function onMobDespawn( mob )
    -- Despawn pets..
	DespawnMob( 17506671 );
	DespawnMob( 17506672 );
	DespawnMob( 17506673 );
	DespawnMob( 17506674 );
    
    -- Reset popped pet var..
    mob:setExtraVar( 0 );
end
